package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @PackageName: com.itheima.controller
 * @ClassName: RoleController
 * @Author: AdamMing
 * @Date: 2020/3/19 15:49
 * @Description: 角色业务controller
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Reference
    private RoleService roleService;

    //查询所有
    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            List<Role> list = roleService.findAll();
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            //服务请求失败
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);

        }
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Role role, Integer[] permissionIds, Integer[] menuIds){
        try {
            roleService.add(role,permissionIds,menuIds);
            return new Result(true,MessageConstant.ADD_MENU_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_MENU_FALL);
        }
    }

    /** findPage 分页查询*/
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return roleService.findPage(queryPageBean);
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Role role = roleService.findById(id);
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,role);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    /** 修改角色信息及其对应的权限和菜单信息*/
    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role, Integer[] permissionIds, Integer[] menuIds){
        try {
            roleService.edit(role,permissionIds,menuIds);
            return new Result(true,MessageConstant.EDIT_ROLE_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_ROLE_FALL);
        }
    }

    @RequestMapping("/delete")
    public Result delete(Integer roleId){
        try {
            roleService.delete(roleId);
            return new Result(true,MessageConstant.DELETE_ROLE_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_ROLE_FALL);
        }
    }
}
